// 判断日期是否重叠
export const checkDateOverlap = (rangeDate) => {
  for (let i = 0; i < rangeDate.length; i++) {
    const rangeA = rangeDate[i];

    // 忽略无效的时间段
    if (!rangeA || rangeA.length !== 2) {
      continue;
    }

    for (let j = i + 1; j < rangeDate.length; j++) {
      const rangeB = rangeDate[j];

      // 忽略无效的时间段
      if (!rangeB || rangeB.length !== 2) {
        continue;
      }

      if (
        (rangeA[0] <= rangeB[0] && rangeB[0] <= rangeA[1]) ||
        (rangeA[0] <= rangeB[1] && rangeB[1] <= rangeA[1]) ||
        (rangeB[0] <= rangeA[0] && rangeA[0] <= rangeB[1]) ||
        (rangeB[0] <= rangeA[1] && rangeA[1] <= rangeB[1])
      ) {
        return true; // 时间段重叠
      }
    }
  }

  return false; // 时间段不重叠
};

// 判断时间是否重叠
export const checkTimeOverlap = (rangeTime) => {
  for (let i = 0; i < rangeTime.length; i++) {
    const timeA = rangeTime[i];

    // 忽略无效的时间段
    if (!timeA || timeA.length !== 2) {
      continue;
    }

    for (let j = i + 1; j < rangeTime.length; j++) {
      const timeB = rangeTime[j];

      // 忽略无效的时间段
      if (!timeB || timeB.length !== 2) {
        continue;
      }

      const startTimeA = timeA[0];
      const endTimeA = timeA[1];
      const startTimeB = timeB[0];
      const endTimeB = timeB[1];

      if (
        (startTimeA <= startTimeB && startTimeB <= endTimeA) ||
        (startTimeA <= endTimeB && endTimeB <= endTimeA) ||
        (startTimeB <= startTimeA && startTimeA <= endTimeB) ||
        (startTimeB <= endTimeA && endTimeA <= endTimeB)
      ) {
        return true; // 时间重叠
      }
    }
  }

  return false; // 时间不重叠
};

export const isTimeRangeIncluded = (activityTime, couponTime) => {
  // 判断第二个时间段是否在第一个时间段中
  if (activityTime && couponTime) {
    const activityStart = new Date(activityTime[0]);
    const activityEnd = new Date(activityTime[1]);
    const couponStart = new Date(couponTime[0]);
    const couponEnd = new Date(couponTime[1]);
    return couponStart >= activityStart && couponEnd <= activityEnd;
  }
};

export const limitNumber = (value) => {
  // 限制输入整数
  if (typeof value === "string") {
    return !isNaN(Number(value)) ? value.replace(/^(0+)|[^\d]/g, "") : "";
  } else if (typeof value === "number") {
    return !isNaN(value) ? String(value).replace(/^(0+)|[^\d]/g, "") : "";
  } else {
    return "";
  }
};
